package a630wj.recycleview630wj.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import java.util.ArrayList;

import a630wj.recycleview630wj.R;
import a630wj.recycleview630wj.fragment.ArticleFragment;
import a630wj.recycleview630wj.fragment.CourseFragment;
import a630wj.recycleview630wj.fragment.VideoFragment;


public class MainActivity extends AppCompatActivity  {
    private LinearLayout layout1,layout2,layout3,layout4;
    private ImageButton ib1,ib2,ib3,ib4;
    private ViewPager vp;
    private ArrayList<Fragment> listfragment;
    private FragmentPagerAdapter fpadapter;
//    private RecyclerView recyclerView;
//    private RecyclerView.LayoutManager layoutManager;
//    private MyAdapter myAdapter;
//    private List<ArticleBean> list = null;
//     ArticleListener articleListener = new ArticleListener() {
    //@Override
//        public void onResponse(List<ArticleBean> listbean) {
//    list = listbean;
//            myAdapter.setList(list);
//        }



    //       @Override
    //     public void onFail(String msg) {
//            Toast.makeText(MainActivity.this, "fail:" + msg, Toast.LENGTH_SHORT).show();
    //       }
//    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initEvents();
        initFragment();
//        ArticleModel articleModel=new ArticleModel();
//        articleModel.getResultList("article",2,"jpp4d0pav3trtv4thnq7afs7t1",articleListener);

    }
    View.OnClickListener onClickListener=new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            resetImage();
            switch (v.getId()) {
                case R.id.layout1:selectTab(0);break;
                case R.id.layout2:selectTab(1);break;
                case R.id.layout3:selectTab(2);break;
                case R.id.layout4:selectTab(3);break;
            }
        }
    };
    private void initEvents() {
        layout1.setOnClickListener(onClickListener);
        layout2.setOnClickListener(onClickListener);
        layout3.setOnClickListener(onClickListener);
        layout4.setOnClickListener(onClickListener);
    }
    private void resetImage()
    {
        ib1.setImageResource(R.drawable.weixin_normal);
        ib2.setImageResource(R.drawable.friend_normal);
        ib3.setImageResource(R.drawable.address_normal);
        ib4.setImageResource(R.drawable.settings_normal);
    }
    private void selectTab(int i)
    {
        switch (i)
        {
            case 0:
                ib1.setImageResource(R.drawable.weixin_pressed);
                break;
            case 1:ib2.setImageResource(R.drawable.friend_pressed);
                break;
            case 2:ib3.setImageResource(R.drawable.address_pressed);
                break;
            case 3:ib4.setImageResource(R.drawable.settings_pressed);
                break;
        }
        vp.setCurrentItem(i);
    }

    private void initFragment() {
        listfragment = new ArrayList<>();
        listfragment.add(new ArticleFragment());
        listfragment.add(new CourseFragment());
        listfragment.add(new VideoFragment());
        fpadapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public android.support.v4.app.Fragment getItem(int position) {
                return listfragment.get(position);
            }

            @Override
            public int getCount() {
                return listfragment.size();
            }


        };
        vp.setAdapter(fpadapter);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
               // vp.setCurrentItem(position);
                resetImage();
                selectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void init() {
        layout1=(LinearLayout)findViewById(R.id.layout1);
        layout2=(LinearLayout)findViewById(R.id.layout2);
        layout3=(LinearLayout)findViewById(R.id.layout3);
        layout4=(LinearLayout)findViewById(R.id.layout4);
        ib1=(ImageButton)findViewById(R.id.imageButton1);
        ib2=(ImageButton)findViewById(R.id.imageButton2);
        ib3=(ImageButton)findViewById(R.id.imageButton3);
        ib4=(ImageButton)findViewById(R.id.imageButton4);
        vp=(ViewPager)findViewById(R.id.viewpager);
/*
        recyclerView=(RecyclerView) findViewById( R.id.recyclerview);
        //创建默认的线性LayoutManager
        layoutManager=new LinearLayoutManager(this);
        //设置布局
        recyclerView.setLayoutManager(layoutManager);
        //设置iyem的高度
        recyclerView.setHasFixedSize(true);
        //创建适配器
        //list=getData();
        myAdapter=new MyAdapter(this);
        myAdapter.setList(list);
        recyclerView.setAdapter(myAdapter);
*/
    }
    }

